﻿@using IOA.MES.DataAccess.VModel
@{
    ViewBag.Title = "工单分档规则配置";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";

    var schemas = ViewBag.Schemas as List<VBinningSchema>;
}
@model VWorkOrderBinningSchema

@section Contents{
    <style>
        html {
            background-color: #FFF;
        }

        .layui-form-label {
            width: auto;
        }

        .layui-table-cell {
            height: auto;
        }

        .layui-form-item .layui-input-inline {
            width: 300px;
        }

        .red {
            color: red;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <label class="layui-form-label">{{lang.Field.GradingScheme}}：</label>
                    <div class="layui-input-inline">
                        <input type="hidden" name="OrderNo" value="@ViewBag.OrderNo" />
                        <input type="hidden" name="WorkOrderNo" value="@ViewBag.WorkOrderNo" />
                        <select id="selSchemas" name="BinningSchemeId" lay-filter="selSchemas" lay-search="">
                            <option value="0">选择分档方案</option>
                            @foreach (var item in schemas)
                            {
                                <option value="@item.PkId" @(Model != null && Model.BinningSchemeId == item.PkId ? " selected" : "")>@item.SchemaName</option>
                            }
                        </select>
                    </div>
                    <div class="layui-input-inline">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="submit">{{lang.Field.Submit}}</button>
                        <button class="layui-btn layui-btn-normal" id="btnMgr">{{lang.Field.RulesManager}}</button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['form', 'layer', 'index', 'laydate', 'table'], function () {
                var layer = layui.layer,
                    form = layui.form,
                    table = layui.table;

                form.on('select(selSchemas)', function (data) {
                    queryPowers();
                });

                var options = {
                    elem: '#table'
                    , url: '/ParaSetting/QueryBinningPowers'
                    , method: "post"
                    , where: { schemaId: 0 }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers" }
                        , { field: "NameplateModel", title: '铭牌型号' }
                        , {
                            title: '额定功率', templet: function (d) {
                                return d.Power + "W";
                            }
                        }
                        , {
                            title: '电流档位', templet: function (d) {
                                var powers = new Array();
                                for (i = 0; i < d.Currents.length; i++) {
                                    powers.push(d.Currents[i].LevelName + "：" + d.Currents[i].CurrentMin + "A - " + d.Currents[i].CurrentMax + "A");
                                }
                                return powers.join("<br/>");
                            }
                        }
                    ]]
                };

                queryPowers();
                function queryPowers() {
                    options.where.schemaId = parseInt($("#selSchemas").val().trim());

                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }

                $(document).on("click", "#btnMgr", function () {
                    parent.parent.layui.index.openTabsPage("/ParaSetting/BinningSchemeMgr", "分档规则");
                });

                form.on('submit(submit)', function (data) {
                    console.log(data.field);
                    var loadId = layer.load();
                    $.ajax({
                        url: "/ParaSetting/SaveWorkOrderBinningSchema",
                        data: {
                            model: data.field
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            layer.msg("保存成功！", { icon: 6, time: 1500 });
                        }
                    });
                    return false;
                });
            });
        }
    </script>
}
